package main

import (
	"encoding/json"
	"fmt"
	"reflect"
)

type Student struct {
	Name   string `info:"name" mes:"pen_name"`
	Number string `info:"number" mes:"smber"`
	Grade  int    `info:"grade"`
}

func FindTage(arg interface{}) {
	//先获得这个结构体的全部信息Elem
	t := reflect.TypeOf(arg).Elem()

	//按结构体一行一行便遍历
	for i := 0; i < t.NumField(); i++ {
		//获得第i的标签，获得get
		stringinfo := t.Field(i).Tag.Get("info")
		stringmes := t.Field(i).Tag.Get("mes")
		fmt.Printf("strinof: %s, strmes: %s\n", stringinfo, stringmes)
	}
}

type Movie struct {
	Name   string   `json:"电影名字"`
	Price  int      `json:"票价"`
	Date   string   `json:"上映时间"`
	Actors []string `json:"主演"`
}

func main() {
	fmt.Println("hello json")
	movie := Movie{"夏洛特烦恼", 40, "2017-6-20", []string{"沈腾", "马丽"}}
	fmt.Println(movie)

	//将movie编码，进行json-->struct
	jsonstr, err := json.Marshal(movie)
	if err != nil {
		fmt.Println("json marshal fial")
		return
	}
	//jsonstr: {"电影名字":"夏洛特烦恼","票价":40,"上映时间":"2017-6-20","主演":["沈腾","马丽"]}
	fmt.Printf("jsonstr: %s\n", jsonstr)

	//解码unshrmal,将jsonstr-->struct
	//jsonstr: {"电影名字":"夏洛特烦恼","票价":40,"上映时间":"2017-6-20","主演":["沈腾","马丽"]}

	mymovie := Movie{}
	err = json.Unmarshal(jsonstr, &mymovie) //将jsonstr解码到mymovie中
	fmt.Println(mymovie)
	fmt.Printf("%v\n", mymovie)

	GDUT := Student{"t_leonaylin", "E3-0298", 1}
	FindTage(&GDUT)

}
